poi导出excel设置样式

您所在的位置:网站首页 poi导出excel 设置单元格格式 poi导出excel设置样式

poi导出excel设置样式

2023-04-05 20:51| 来源: 网络整理| 查看: 265

由于要利用poi导出excel(XSSFWorkbook),而且要添加样式,搜索其他的结果无非都是颜色值,经查询的结果,做一下总结:

1、设置背景色,要用  style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());

使用 style.setFillBackgroundColor(bg);方法总是出现一个黑块,所以改为上面的写法,结果正确

颜色的问题,可以在 IndexedColors 中查到,是个枚举

2、有合并单元格的情况下,给一行直接设置背景颜色,并没有起到效果,所以在 createCell 时,包装了一个方法

1 /** 2 * 创建cell 此方法目的,是要给所有的cell加上边框和背景色 3 * @param workbook 工作薄 4 * @param row 行 5 * @param index 下标 6 * @return 7 */ 8 public XSSFCell createCell(XSSFWorkbook workbook,XSSFRow row,int index,String ... args) { 9 CellStyle style=SheetStyle.getDefaultCellStyle(workbook); 10 XSSFCell cell = row.createCell(index); 11 12 //大于3,因为前边几行不需要样式 13 if(row.getRowNum()>3) { 14 //偶数行为Lime,奇数行为Yellow 15 if(this.staffIndex%2==0) { 16 SheetStyle.setCellStyleLime(style); 17 }else { 18 SheetStyle.setCellStyleYellow(style); 19 } 20 } 21 row.setRowStyle(style); 22 cell.setCellStyle(style); 23 return cell; 24 }

把样式同时赋值给RowStyle和CellStyle,导出的效果理想

3、需要有合并单元格的情况,必须先创建合并单元格,然后在填值

CellRangeAddress callRangeAddress5 = new CellRangeAddress(start,end,0,0);//起始行,结束行,起始列,结束列 sheet.addMergedRegion(callRangeAddress5);

4、spring mvc导出excel文件,Controller部分的代码:

主要用来设置导出的文件名

/** * excel排班 * @param request * @param response * @return * @throws IOException */ @SuppressWarnings("resource") @RequestMapping(value = "exportExcel") public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws IOException { Map result=schedulingService.downloadScheduling(request.getParameter("x")); String name="导出结果"+result.get("month"); byte [] bytes=(byte[]) result.get("data"); response.setContentType("application/binary;"); response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(name+".xlsx", "UTF-8")); response.setContentLength(bytes.length); response.getOutputStream().write(bytes); response.getOutputStream().flush(); response.getOutputStream().close(); }

 

将SheetStyle完整代码贴出,有需要的可以拿来复制

import org.apache.poi.ss.usermodel.BorderStyle; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.HorizontalAlignment; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.VerticalAlignment; import org.apache.poi.xssf.usermodel.XSSFWorkbook; /** * 设置excel的样式类 * */ public class SheetStyle { public static void setColumnWidth() { } /** * 水平居中、垂直居中 * 字体:宋体 * 字体大小:16号 * 加粗 * @param workbook * @return */ public static CellStyle getStyle(XSSFWorkbook workbook) { CellStyle cellstyle=workbook.createCellStyle(); cellstyle.setAlignment(HorizontalAlignment.CENTER);//水平居中 cellstyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中 Font font=workbook.createFont();//字体 font.setFontName("宋体");//字体 font.setFontHeightInPoints((short)16);//字号 font.setBold(true);//加粗 cellstyle.setFont(font); setBorderStyle(cellstyle); return cellstyle; } /** * 获取默认的cell表格样式,加边框,水平居中,垂直居中 * @param workbook * @return */ public static CellStyle getDefaultCellStyle(XSSFWorkbook workbook) { CellStyle style=workbook.createCellStyle(); style.setAlignment(HorizontalAlignment.CENTER);//水平居中 style.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中 setBorderStyle(style); return style; } /** * 边框样式 * @param style */ public static void setBorderStyle(CellStyle style) { style.setBorderBottom(BorderStyle.THIN); //下边框 style.setBorderLeft(BorderStyle.THIN);//左边框 style.setBorderTop(BorderStyle.THIN);//上边框 style.setBorderRight(BorderStyle.THIN);//右边框 } /** * 奇数行 * 背景颜色为黄色 * @param style */ public static void setCellStyleYellow(CellStyle style) { style.setFillForegroundColor(IndexedColors.YELLOW.getIndex()); style.setFillPattern(FillPatternType.SOLID_FOREGROUND); } /** * 偶数行 * 背景颜色为LIME * @param style */ public static void setCellStyleLime(CellStyle style) { style.setFillForegroundColor(IndexedColors.LIME.getIndex()); style.setFillPattern(FillPatternType.SOLID_FOREGROUND); } /** * 字体设置红色 * @param workbook * @param style */ public static void setFontRedColor(XSSFWorkbook workbook,CellStyle style) { Font font=workbook.createFont();//字体 font.setColor(IndexedColors.RED.getIndex()); style.setFont(font); } }

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3